<?


class CTRLlocation extends CONTROL {

	function __construct( $vars ) {
		global $Object;
		
		$this->_addvars( $vars );

		if( $this->action != "list" )
			$this->object = $Object = $this->create_object();
		
	}

#	Edit Object.
	function filter_edit() {
		global $DB, $Engine, $Object;
		
		$type = $this->vars->type;
		
		// check for countries
		$countries = $DB->filter('country');
		
		$country_select		= '';
		if ( $countries ) {
			
			foreach ($countries as $country) {
				$short	= json_decode($country['meta']);
				$ctry[$country['name']] = $short->ctry;
			}
			$country_select	= "new Select({ type: '{$this->vars->type}', descr:'country', objid: {$Object->id}, field:'meta.country', value: '{$Object->get( 'country' )}', opts: " . json_encode( $ctry ) . " }),";
		}
		
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer $type '". (( $Object->get( "nameid" ) )? $Engine->text->get( $Object->get( "nameid" ) ) : $Object->id ) . "'\" ),
			new Textfield({ descr:'name $type', type:'$type', parentid:{$Object->id}, textid:". $Object->get( "nameid" ) .", values:". json_encode( $Engine->text->filter( $Object->get( "nameid" ) )) ." }),
			new Htmlfield({ descr:'text $type', type:'$type', parentid:{$Object->id}, textid:". $Object->get( "textid" ) .", values:". json_encode( $Engine->text->filter($Object->get( "textid" ), 'long' )) ." }),
			new Dropbox({ descr:'Beelden locatie', type:'media.img', parenttype:'$type', action:'win', blank:'add image', id:{$Object->id}, uptype:'media', floats:". $this->filter_media( 'media.img' ) ." }),
			new Options({ descr:'Opties', floats: [
				" . $country_select . "
				new Ajxfield({ type:'$type', descr:'address', objid:{$Object->id}, id:{$Object->id}, field:'meta.ll', value:'{$Object->get('address')}', requestfunc:'pull' }),
				new Ajxfield({ type:'$type', descr:'link', objid:{$Object->id}, field:'meta.link', value:'{$Object->get('link')}' }),
				new Shift({ type:'$type', descr:'Is public', value:" . $Object->get( 'active' ) . ", field:'active', objid:{$Object->id} }),
				new Rank({ type:'$type', descr:'Rank', value:" . $Object->get( 'rank' ) . ", field:'rank', objid:{$Object->id} })
			]})
		]);";

		
	}

#	Render media floats
	function filter_media( $objtype ) {
		global $DB, $Object;
		
		$result = array();
		
		if( $images = $Object->media->filter( $objtype ))
			foreach( $images as $img ) {
				$result[] = array( "id"=> $img['ID'], "type"=> $objtype, "name"=> $objtype, "list"=> array( $img['ID'], json_decode( $img[ 'meta' ])->raw ));
			}
		
		return json_encode( $result );
	}

	
	
#	Shift (alter column value).
	function filter_ajax() {
		global $DB, $Request;
		
		if( $Request->vars->requestfunc )
			$comment = $this->{ $Request->vars->requestfunc }();
		
		else 
			$comment = ( $Request->ajax() )? "option modified":"warning: failed";

		return $this->comment( $comment );
	}
	
	
	
#	Google maps interaction
	function pull() {
		global $Request, $Object, $DB;
		
		
		
		$country = engine_get( 'local.country' );
		if ( $Object->get( 'country' ))
			$country = strtolower($Object->get('country'));
		
		$location	= file_get_contents('http://maps.google.com/maps/geo?q=' . urlencode($Request->vars->value) . '+' . $country . '&key=' . engine_get( 'mod.google.api.code' ) . '&oe=utf8');
		$location	= json_decode($location);
				
		// check address
		if ($location->Status->code == 200) {
		
			// get ll and save to db
			$latLng 	= $location->Placemark[0]->Point->coordinates;
			
			$Object->values[ 'meta' ]->ll		= $latLng[1] . ',' . $latLng[0];
			$Object->values[ 'meta' ]->address	= $Request->vars->value;
			$Object->set( "meta", $Object->values[ 'meta' ], true );
			
			// return succes
			return 'address found: ' . $latLng[1] . ',' . $latLng[0];
			
		} else {
			
			// return false
			return 'address not found';
		
		}
	}
}


?>